﻿using Ledict.ConditionExtract.Base;
using Ledict.ConditionExtract.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace Ledict.ConditionExtract.Frm.NodeTree
{

    public partial class RuleConfig : BaseForm
    {
        /// <summary>
        /// 逻辑分段是否可以进行全文提取选择
        /// </summary>
        public bool IsCanChecked { get; set; }

        /// <summary>
        /// 逻辑分段还是要素中心
        /// </summary>
        public int TabSelect { get; set; }

        /// <summary>
        /// 逻辑分段结构树
        /// </summary>
        public string LuoJiFDContent { get; set; }

        public ElementExtractSource ElementExtractModel { get; set; }
        private config cf = new config();
        public bool IsAdd { get; set; }
        public int typed;
        List<string> nodes = new List<string>();
        List<string> nodesTag = new List<string>();
        private string nodesTaglist;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="configModel">配置模型</param>
        /// <param name="tabSelect">选择类型0分段1要素</param>
        /// <param name="type">0添加1修改</param>
        public RuleConfig(config configModel, int tabSelect, int type)
        {
            InitializeComponent();
            cf = configModel;
            lblWenShuName.Text = configModel.bookName;
            TabSelect = tabSelect;
            typed = type;
        }




        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RuleConfig_Load(object sender, EventArgs e)
        {
            if (TabSelect == 0)
            {
                if (IsCanChecked == true)
                {
                    cbxIsQuanWen.Enabled = true;
                }
                else
                {
                    cbxIsQuanWen.Enabled = false;
                }
            }
            else if (TabSelect == 1)
            {
                cbxDuanLuoType.Enabled = cbxTvFD.Enabled = false;
                if (!string.IsNullOrEmpty(LuoJiFDContent))
                {
                    XmlToTree(LuoJiFDContent, cbxTvFD.TreeView);
                }
                cbxTvFD.TreeView.CheckBoxes = true;
                cbxTvFD.TreeView.AfterSelect += TreeView_AfterSelect;
                cbxTvFD.TreeView.AfterCheck += TreeView_AfterCheck;
            }
            //添加
            if (typed == 0)
            {
                if (TabSelect == 0)
                {
                    panelYaoSu.Visible = false;

                }
                else
                {
                    panelYaoSu.Visible = true;
                }
            }//修改
            else if (typed == 1)
            {
                if (TabSelect == 0)
                {
                    panelYaoSu.Visible = false;
                    if (IsCanChecked == true)
                    {
                        cbxIsQuanWen.Enabled = true;
                        cbxIsQuanWen.Text = cf.IsQuanWen;

                    }
                    else
                    {
                        cbxIsQuanWen.Enabled = false;
                        cbxIsQuanWen.Text = "是";
                    }
                   
                }
                else
                {
                    panelYaoSu.Visible = true;
                    if (cf.IsQuanWen == "是")
                    {
                        cbxIsQuanWen.SelectedIndex = 0;
                    }
                    else if (cf.IsYuanWen == "是")
                    {
                        cbxIsYuanWen.SelectedIndex = 0;
                    }
                    else if (cf.IsLuJiDuan == "是")
                    {
                        cbxIsLuoJiDuan.SelectedIndex = 0;
                        cbxDuanLuoType.Text = cf.TiQuType;
                    }
                }
                cbxUseML.Text = cf.IsUseML;
            }
        }

        private void TreeView_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Checked)
            {
                if (!nodes.Contains(e.Node.Text.ToString()))
                {
                    nodes.Add(e.Node.Text.ToString());
                }
                if (!nodesTag.Contains(e.Node.Tag.ToString()))
                {
                    nodesTag.Add(e.Node.Tag.ToString());
                }
            }
            else if (!e.Node.Checked)
            {
                if (nodes.Contains(e.Node.Text.ToString()))
                {
                    nodes.Remove(e.Node.Text.ToString());
                }
                if (nodesTag.Contains(e.Node.Tag.ToString()))
                {
                    nodesTag.Remove(e.Node.Tag.ToString());
                }

            }
            if (nodes.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (string item in nodes)
                {
                    sb.Append(item + ",");
                }
                cbxTvFD.Text = sb.Remove(sb.Length - 1, 1).ToString();
            }
            if (nodesTag.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (string item in nodesTag)
                {
                    sb.Append(item + ",");
                }
                nodesTaglist = sb.Remove(sb.Length - 1, 1).ToString();
            }
        }

        private void TreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            cbxTvFD.Text = cbxTvFD.TreeView.SelectedNode.Text.ToString();
        }

        public void XmlToTree(string xml, TreeView tv)
        {
            tv.Nodes.Clear();
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            XmlElement root = doc.DocumentElement;
            TreeNode tn = new TreeNode() { Text = root.GetAttribute("explain"), Tag = root.GetAttribute("code") };
            foreach (XmlNode subXmlNod in root.ChildNodes)
            {
                TreeNode tn1 = new TreeNode() { Text = ((XmlElement)subXmlNod).GetAttribute("zh_cn"), Tag = ((XmlElement)subXmlNod).GetAttribute("code") };
                tn.Nodes.Add(tn1);
                TransXml(subXmlNod.ChildNodes, tn1, 0);
            }
            tv.Nodes.Add(tn);
        }

        public void TransXml(XmlNodeList xmlNodes, TreeNode parent, int tag)
        {
            foreach (XmlNode xmlNod in xmlNodes)
            {
                TreeNode subNod = new TreeNode() { Text = ((XmlElement)xmlNod).GetAttribute("zh_cn"), Tag = ((XmlElement)xmlNod).GetAttribute("code") };
                parent.Nodes.Add(subNod);
                if (xmlNod.ChildNodes.Count > 0)
                {
                    TransXml(xmlNod.ChildNodes, subNod, 0);
                }
            }
        }


        /// <summary>
        /// 全文改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbxIsQuanWen_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (panelYaoSu.Visible == true)
            {
                if (cbxIsQuanWen.SelectedIndex == 0)
                {
                    cbxUseML.Enabled = cbxIsYuanWen.Enabled = cbxIsLuoJiDuan.Enabled = false;
                    cbxUseML.SelectedIndex = cbxIsYuanWen.SelectedIndex = cbxIsLuoJiDuan.SelectedIndex = -1;
                }
                else
                {
                    cbxUseML.Enabled = cbxIsYuanWen.Enabled = cbxIsLuoJiDuan.Enabled = true;
                }
            }
        }

        /// <summary>
        /// 原文改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbxIsYuanWen_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (panelYaoSu.Visible == true)
            {
                if (cbxIsYuanWen.SelectedIndex == 0)
                {
                    cbxUseML.Enabled = cbxIsQuanWen.Enabled = cbxIsLuoJiDuan.Enabled = false;
                    cbxUseML.SelectedIndex = cbxIsQuanWen.SelectedIndex = cbxIsLuoJiDuan.SelectedIndex = -1;
                }
                else
                {
                    cbxUseML.Enabled = cbxIsQuanWen.Enabled = cbxIsLuoJiDuan.Enabled = true;
                }
            }
        }

        /// <summary>
        /// 逻辑段改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbxIsLuoJiDuan_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (panelYaoSu.Visible == true)
            {
                if (cbxIsLuoJiDuan.SelectedIndex == 0)
                {
                    cbxUseML.Enabled = cbxIsQuanWen.Enabled = cbxIsYuanWen.Enabled = false;
                    cbxUseML.SelectedIndex = cbxIsQuanWen.SelectedIndex = cbxIsYuanWen.SelectedIndex = -1;
                    cbxDuanLuoType.Enabled = cbxTvFD.Enabled = true;
                    cbxDuanLuoType.SelectedIndex = 0;
                }
                else
                {
                    cbxUseML.Enabled = cbxIsQuanWen.Enabled = cbxIsYuanWen.Enabled = true;
                    cbxDuanLuoType.Enabled = cbxTvFD.Enabled = false;
                    cbxDuanLuoType.SelectedIndex = -1;
                    cbxTvFD.Text = "";
                }
            }
        }

        /// <summary>
        /// 取消按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCanel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        /// <summary>
        /// 确定按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            ElementExtractModel = new ElementExtractSource();
            if (TabSelect == 0)
            {
                ElementExtractModel.EES_ElementType = 1;
                ElementExtractModel.EES_SourceType = cbxIsQuanWen.Enabled == true ? cbxIsQuanWen.SelectedIndex == 0 ? 4 : 3 : 0;
                ElementExtractModel.EES_IsML = cbxUseML.SelectedIndex == 0 ? 1 : 0;
            }
            else if (TabSelect == 1)
            {
                if (cbxIsQuanWen.SelectedIndex == -1 && cbxIsYuanWen.SelectedIndex == -1 && cbxIsLuoJiDuan.SelectedIndex == -1 && cbxUseML.SelectedIndex == -1)
                {
                    MessageBox.Show("请选择完整后再试！");
                    return;
                }
                if (cbxIsLuoJiDuan.SelectedIndex != -1 && cbxIsLuoJiDuan.SelectedIndex == 0 && string.IsNullOrEmpty(cbxTvFD.Text))
                {
                    MessageBox.Show("请选择逻辑段编号后再试！");
                    return;
                }
                if (cbxIsLuoJiDuan.SelectedIndex != 0 && cbxIsQuanWen.SelectedIndex != 0 && cbxIsYuanWen.SelectedIndex != 0 && cbxUseML.SelectedIndex != 0)
                {
                    MessageBox.Show("请至少配置一项为是后再试！");
                    return;
                }
                ElementExtractModel.EES_ElementType = 2;
                ElementExtractModel.EES_SourceType = cbxIsQuanWen.SelectedIndex == 0 ? 4 :
                    cbxIsYuanWen.SelectedIndex == 0 ? 3 :
                    cbxIsLuoJiDuan.SelectedIndex == 0 ? cbxDuanLuoType.SelectedIndex == 0 ? 1 : 2 : -1;
                ElementExtractModel.EES_IsML = cbxUseML.SelectedIndex == 0 ? 1 : 0;
                if (cbxDuanLuoType.Enabled == true && cbxTvFD.Text != null)
                {
                    ElementExtractModel.EES_ParagraphName = nodesTaglist;
                    ElementExtractModel.EES_Remark = cbxTvFD.Text.ToString();
                }

            }
            DialogResult = DialogResult.Yes;
            this.Close();
        }

        /// <summary>
        /// 机器学习
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbxUseML_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxUseML.SelectedIndex == 0)
            {
                cbxIsQuanWen.Enabled = cbxIsQuanWen.Enabled = cbxIsLuoJiDuan.Enabled = false;
                cbxIsQuanWen.SelectedIndex = cbxIsQuanWen.SelectedIndex = cbxIsLuoJiDuan.SelectedIndex = -1;
            }
            else
            {
                cbxIsQuanWen.Enabled = cbxIsQuanWen.Enabled = cbxIsLuoJiDuan.Enabled = true;
            }
        }

    }

    public class config
    {
        /// <summary>
        /// 文书名称
        /// </summary>
        public string bookName { get; set; }

        /// <summary>
        /// 是否原文提取
        /// </summary>
        public string IsYuanWen { get; set; }

        /// <summary>
        /// 是否全文提取
        /// </summary>
        public string IsQuanWen { get; set; }

        /// <summary>
        /// 是否逻辑段提取
        /// </summary>
        public string IsLuJiDuan { get; set; }

        /// <summary>
        /// 逻辑段提取类型
        /// </summary>
        public string TiQuType { get; set; }

        /// <summary>
        /// 逻辑段编号
        /// </summary>
        public string LuJiDuanCode { get; set; }

        /// <summary>
        /// 主键
        /// </summary>
        public string EES_ID { get; set; }

        /// <summary>
        /// 是否使用机器学习
        /// </summary>
        public string IsUseML { get; set; }
    }
}
